package com.briup.jd2511.demo02.mapper;

import com.briup.jd2511.demo02.bean.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface UserMapper {

    /**
     * 根据id删除
     * @param id
    */
    @Delete("delete from es_user where id=#{id}")
    void deleteById(Integer id);

    /**
     * 新增用户用
     * @param user
     *
     * 如果要使用新增之后获得主键值，需要添加Options注解
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into es_user(username, password, phone, avatar, gender, status, birthday, register_time) values(#{username}, #{password}, #{phone}, #{avatar}, #{gender}, #{status}, #{birthday}, #{registerTime})")
    void insert(User user);

    /**
     * 更新用户
     * @param user
     */
    @Update("""
                update es_user
                set username=#{username},
                    phone=#{phone},
                    avatar=#{avatar},
                    gender=#{gender},
                    status=#{status},
                    birthday=#{birthday}
                where id = #{id}
            """)
    void update(User user);

    /**
     * 根据id查询单个用户信息
     * @param id
     * @return
     */
    @Select("""
               select id, username, password,
               phone, avatar, gender,
               status, birthday, register_time
               from es_user
               where id = #{id}
             """)
    // 如果不开启驼峰命名，那需要手动使用mybatis指定结果映射
    @Results({
            @Result(column = "register_time",property = "registerTime")
    })
    User getById(Integer id);

    @Select("""
            select id, username, password, phone, avatar, gender, status, birthday, register_time 
            from es_user 
            where username like concat('%',#{username},'%')
            and status = #{status}
            and register_time between #{startTime} and #{endTime}
            order by register_time desc
            """)
    List<User> getByCondition(String username,
                              String status,
                              @Param("startTime") LocalDate begin,
                              @Param("endTime") LocalDate end);
}
